/**
 * 自动保存设置
 */
 var autoSaveTime = 300000;
 var baseUrl = location.href.replace(/\/[^\/]+$/ig,"/");
 var submitFlag = false;
 $(function(){
    if(!_judgeExpertUser){
        normalInit();
    } else {
        expertInit();
    }
    $(":radio[name='XWFBDetail1$ifCharacterProj']").click(exchangeShow);
    exchangeShow();
    subject2countTotal();
    subject3countTotal();
    
    
    //计算分数
    var total = $("#Hiddentotal").val();    var curtotal = $("#Hiddencurtotoal").val();
    var curtotal = countCurrent();
    $("#labtotal").text(total);    $("#labcurtotoal").text(~~curtotal-1);    $("#spaceused1").text(curtotal);    $("#spaceused1").progressBar({ max: total, textFormat: 'fraction',showText: false});    
    
    $("form").submit(function(e){
        console.log(e);
        var total = countTotal();
        var current = countCurrent();
        $("#Hiddentotal").val(total);        $("#Hiddencurtotoal").val(current);        
        if(submitFlag){//提交按钮
            if(current < total){
                if(!confirm("信息没有填写完整({0}/{1})，确定提交？".format(current,total))){
                    submitFlag = false;
                    return false;
                }
            }
        }
        submitFlag = false;
        return true;
    });
 });
 
function normalInit(){
    if("1"!=$("#Hiddenstate").val()){
        setTimeout(autoSave,autoSaveTime);
        $("#Button1").click(function(){
            if(confirm("提交后将不能再次修改，你确认要提交吗？")){
                submitFlag = true;
                return true;
            }
            return false;
        });
    } else {
        $("#btnSave,#Button1").hide();
    }
 } 
 
 
 function autoSave(){
    var asi = $("<iframe name=\"autoSaveIframe\" width='100%' style='display:none'></iframe>");
    $("body").append(asi);
    var ajax = $("<input type='hidden' name='ajax' value='true'></input>")
    $("form").attr("target","autoSaveIframe");
    $("form").append(ajax);
    $("#btnSave").click();
    ajax.remove();
    $("form").attr("target","_self");
    asi.load(function(){
        var returnId = $("#HiddenID",window.frames["autoSaveIframe"].document).val();
        var tip = "草稿已经于" + new Date().format("yyyy-MM-dd hh:mm:ss") + "自动保存";
        $("#HiddenID").val(returnId);
        $(".paodai span").eq(1).html(tip).show();
        init();
        asi.remove();
        setTimeout(autoSave,autoSaveTime);
    });
 }
 function printPreview(){
    jQuery.fn.outer = function(){return $("<div></div>").append(this.clone()).html();};
    var child = window.open("about:blank","","height=700,width=960,toolbar =yes,menubar=no,scrollbars=yes, resizable=no, location=no, status=no");
    var link = $("link").clone().outer().replace(/\.\.\//ig,baseUrl + "../");
    var title = "<title>打印预览</title>";
    $("head",child.document).html(title + link);
    var bodyHtml = "";
    var size = $("#container").children().size();
    $("#container").children().each(function(i){
        if(i>1 && i< size - 2){
            bodyHtml += $(this).outer();
        }
    });
    
    var table = "<div class='main'><div style='text-align:center;font-size: 24px;font-family: \"黑体\";font-weight: bold;margin-left:10px'>网站测评指标自查</div><div style='font-size: 12px;padding:3px 20px'>单位名称（盖章）：<span id='dw_name'></span></div><div  style='font-size: 12px;padding:3px 20px'>填报人：</div><div class='container'></div></div>"
    $("body",child.document).html(table);
    $("body",child.document).css("background","#FFF");
    bodyHtml = bodyHtml.replace(/\.\.\//ig,baseUrl + "../") ;
    $(".container",child.document).html(bodyHtml);
    $("#dw_name",child.document).html($("#txtCompanyName").val());
    $("img",child.document).css("visibility","hidden");
    child.print();
 }
 
  function expertInit(){
    //alert(1);  
    $(".tijiao").hide();
    $("a:contains('首页')").attr("href","../ZJCP/ExpertGrade.aspx");
    $("a:contains('删除')").remove();
    $("input[title='点击选择附件']").parent().hide();
    $("*[onclick]").removeAttr("onclick");
 }
 
 function exchangeShow(){
    var ifChracterChecked = $(":radio[name='XWFBDetail1$ifCharacterProj']:checked").val() == "是";
    if(ifChracterChecked){
        $("#chracterProj").show();
    } else {
        $("#chracterProj").hide();
        $("#XWFBDetail1_characterProjName").val("");
        $("#XWFBDetail1_characterProjUrl").val("http://");
    }
 }
 
 
 function countTotal(){
    return countTextTotal() + countRadioTotal() + countCheckboxTotal() + countAttachTotal();
 }
 
 function countTextTotal(){
    return $(":text:visible:enabled").size() - $(":text:visible:enabled[readonly]").size();
 }
 
 function countRadioTotal(){
    var names = [];
    var count = 0;
    $(":radio:visible").each(function(){
        var name = $(this).attr("name");
        if(!names[name]){
            names[name] = true;
            count ++;
        }
    });
    console.log("radioTotal:{0}".format(count));
    return count;
 }
 
 function countCheckboxTotal(){
    var count =  $("#ZfgkDetail1_cblIfqyly_0:visible").size() + 1;
    console.log("CheckboxTotal:{0}".format(count));
    return count;    
 }
 
 function countAttachTotal(){
    return $("table[attach]:visible").size();
 }
 
 function countCurrent(){
    return countTextCurrent() + countRadioCurrent() + countCheckboxCurrent() + countAttachCurrent();
 }
 
 function countTextCurrent(){
    var ignoreValue = [];
    ignoreValue["@vip.changning.sh.cn"] = true;
    ignoreValue["在此输入..."] = true;
    ignoreValue["http://"] = true;
    var count = 0;
    $(":text:visible:enabled").each(function(){
        var value = $(this).val();
		var readonly = $(this).attr("readonly");
        if(value && !ignoreValue[value] && !readonly){
            count ++;
        }
    });
    return count;
 }
 
 function countRadioCurrent(){
    return $(":radio:visible:checked").size();
 }
 
 function countCheckboxCurrent(){
    var count = $(":checkbox[name*='XWFBDetail1$cblXwfbzq']:checked").size() > 0 ? 1:0;
    count += $(":checkbox:visible[name*='ZfgkDetail1$cblIfqyly']:checked").size() > 0 ? 1:0;
    console.log("CheckboxCurrent:{0}".format(count));
    return count;       
 }
 
 function countAttachCurrent(){
    var count = 0;
    $("table[attach]:visible").each(function(){
       if($(this).find("a:contains('删除'):visible").size() > 0){
            count ++;
       }
    });
    return count;
 }
 
 function subject2countTotal(){
    $("#ZfgkDetail1_txtgwxxNum1").attr("readonly",true);
    $("#ZfgkDetail1_txtgwxxNum4").blur(function(){
        $("#ZfgkDetail1_txtgwxxNum1").val(~~($("#ZfgkDetail1_txtgwxxNum2").val()) + ~~($("#ZfgkDetail1_txtgwxxNum3").val()) + ~~($("#ZfgkDetail1_txtgwxxNum4").val()));
    });
 }
 
 
  function subject3countTotal(){
    $("#ZfgkDetail1_txtzfxxNum1").attr("readonly",true);
    $("#ZfgkDetail1_txtzfxxNum4").blur(function(){
        $("#ZfgkDetail1_txtzfxxNum1").val(~~($("#ZfgkDetail1_txtzfxxNum2").val()) + ~~($("#ZfgkDetail1_txtzfxxNum3").val()) + ~~($("#ZfgkDetail1_txtzfxxNum4").val()));
    });
 }